<?php
namespace App\Http\Controllers;
use App\Models\Site;
use Illuminate\Http\Request;
class SiteController extends CommonController
{
    public function index()
    {
        dump('Site');
    }
    public function getAll(Request $request)  {
        $sortBy = [
            'sort' => 'asc',
            'created_at' => 'desc',
        ];
        $query = Site::query();
        foreach ($sortBy as $field => $direction) {
            $query->orderBy($field, $direction);
        }
        $list = $query->where('status',1)->select('id','name','pid')->get()->toArray();
        if(!$list){
            return re_warning('暂无数据',null);
        }else{
            return re_success('操作成功',$list);
        }
    }
    // 示例方法，用于展示后台首页
    public function getlist(Request $request)
    {
        $status = $request->input('status',null);
        $name = $request->input('name',null);
        $sortBy = $request->input('sortBy',[
            'sort' => 'asc',
            'created_at' => 'desc',
        ]);

        $query = Site::query();

        foreach ($sortBy as $field => $direction) {
            $query->orderBy($field, $direction);
        }
        if($status){
            $query->where('status','=',$status);
        }
        if($name){
            $query->where('name','like', '%'.$name.'%');
            $query->orWhere('content','like', '%'.$name.'%');
        }
        $list = $query->get()->toArray();
        if(!$list){
            return re_warning('暂无数据',null);
        }else{
            return re_success('操作成功',$list);
        }
    }
    // 其他可能的方法，如用户管理
    public function create(Request $request)
    {
        // 逻辑代码
        $data = $request->all();
        $data['avatar'] = json_encode($data['avatar']);
        $data['longitude'] = json_encode($data['longitude']);
        $inserted = Site::create($data);
        if ($inserted) {
            // 插入成功
           return  re_success("添加成功",$inserted);
        } else {
            // 插入失败
            return  re_danger("添加失败",null);

        }
    }

    public function update(Request $request)
    {

        $model = new Site();
        $info =   $model->find($request->input('id'))->update($request->all());
        if ($info) {
            return  re_success("修改成功",null);
        } else {
            return  re_danger("修改失败",null);
        }
    }

    public function delete(Request $request){
        $id = $request->input('id');
        $info= Site::destroy($id);
        if ($info) {
            return  re_success("删除成功",null);
        } else {
            return  re_danger("删除失败",null);
        }
    }

    public function info(Request $request)
    {
        $info = Site::find($request->id);
        $info['longitude'] = json_decode($info['longitude']);
        $info['avatar'] = json_decode($info['avatar']);

        if($info){
            return  re_success("添加成功",$info);
        }else{
            return  re_warning("没有该数据",null);

        }
    }


    /**
     * 修改状态
     * @param Request $request
     * @return void
     */
    public function status(Request $request)
    {
        $data = $request->all();
        $model = Site::find($data['id']);
        $model->status = $model->status === 1 ? 2 : 1 ;
        $info =  $model->save();
        if ($info) {
            return  re_success("修改成功",null);
        } else {
            return  re_danger("修改失败",null);
        }


    }

}
